package com.summerbird.mallchat.common.chat.service.adapter;

import com.summerbird.mallchat.common.chat.domain.entity.GroupMember;
import com.summerbird.mallchat.common.chat.domain.enums.GroupRoleEnum;
import com.summerbird.mallchat.common.chat.domain.vo.response.ChatMemberListResp;
import com.summerbird.mallchat.common.user.domain.entity.User;
import com.summerbird.mallchat.common.user.domain.enums.ChatActiveStatusEnum;
import com.summerbird.mallchat.common.user.service.cache.UserCache;
import com.summerbird.mallchat.common.websocket.domain.vo.response.ChatMemberResp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@Component
public class MemberAdapter {

    @Autowired
    private UserCache userCache;

    public static List<ChatMemberResp> buildMember(List<User> list) {
        return list.stream().map(user -> {
            ChatMemberResp resp = new ChatMemberResp();
            resp.setActiveStatus(ChatActiveStatusEnum.ONLINE.getStatus());
            resp.setUid(user.getId());
            resp.setLastOptTime(user.getLastOptTime());
            return resp;
        }).collect(Collectors.toList());
    }

    public static List<ChatMemberListResp> buildMemberList(List<User> memberList) {
        return memberList
                .stream()
                .map(member -> ChatMemberListResp.builder()
                .uid(member.getId())
                .avatar(member.getAvatar())
                .name(member.getName())
                .build())
                .collect(Collectors.toList());
    }

    public static List<ChatMemberListResp> buildMemberList(Map<Long, User> userMap) {
        return buildMemberList(new ArrayList<>(userMap.values()));
    }

    public static List<GroupMember> buildMemberAdd(Long groupId, List<Long> uidList) {
        return uidList.stream().map(uid -> {
            GroupMember member = new GroupMember();
            member.setUid(uid);
            member.setGroupId(groupId);
            member.setRole(GroupRoleEnum.MEMBER.getType());
            return member;
        }).collect(Collectors.toList());
    }
}
